package com.example.bai13;

import com.example.object.MyContact;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import 	android.telephony.SmsManager;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SendsmsActivity extends ActionBarActivity {
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sendsms);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.sendsms_container, new PlaceholderFragment()).commit();
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sendsms, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		EditText editSMS;
		Button btnSendSms;
		TextView txtSendTo;
		MyContact ct=null;
		BroadcastReceiver br;
		public PlaceholderFragment() {
		}

		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_sendsms,
					container, false);
			return rootView;
		}
		@Override
		public void onStart() {
			// TODO Auto-generated method stub
			super.onStart();
			editSMS= (EditText) getActivity().findViewById(R.id.editSMS);
			
			Intent i= getActivity().getIntent();
			Bundle b= i.getBundleExtra("DATA");
		ct=(MyContact) b.get("CONTACT");
			txtSendTo=(TextView) getActivity().findViewById(R.id.txtSendTo);
			String tmp=txtSendTo.getText().toString();
		tmp+=" "+ct.getPhone();
			txtSendTo.setText(tmp);
			
			btnSendSms= (Button) getActivity().findViewById(R.id.btnSendSms);
			
			btnSendSms.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					proccessSendSMS();
				}
			});
		}
		private void proccessSendSMS()
		{
			final SmsManager manager= SmsManager.getDefault();
			Intent mgtsent=  new Intent("ACTION_MSG_SENT");
			final PendingIntent pendingMsgsent= PendingIntent.getBroadcast(getActivity(), 0, mgtsent, 0);
			
			br=new BroadcastReceiver(){

				@Override
				public void onReceive(Context context, Intent intent) {
					// TODO Auto-generated method stub
					
					int result= getResultCode();
					String msg="Send OK";
					if(result!=Activity.RESULT_OK)
					{
						msg="Send Fail";
					}
					Toast.makeText(getActivity()	, msg, Toast.LENGTH_LONG).show();
					
				}};
			
			getActivity().registerReceiver(br, new IntentFilter("ACTION_MSG_SENT"));
			
			manager.sendTextMessage(ct.getPhone(),null , editSMS.getText().toString(), pendingMsgsent, null);
			getActivity().finish();

		}
		@Override
		public void onStop() {
			// TODO Auto-generated method stub
			getActivity().unregisterReceiver(br);
			super.onStop();
		}

	}
	

}
